import { mapState, mapMutations } from 'vuex'
import { Toast } from 'vant'
const cartMixin = {
  methods: {
    _add_cart (item) { // 加入购物车
      const hasItem = this._items.find((el) => el.id === item.id)
      if (hasItem) {
        // 购物车中已经有了该商品
        Toast('购物车中已经有该商品，请勿重复添加')
        return
      }
      this.add_cart(item)
      Toast('添加成功，快去付钱吧')
    },
    ...mapMutations('cart', ['add_cart'])
  },
  computed: {
    ...mapState({
      _items: (state) => state.cart.cart.items
    })
  }
}

export default cartMixin
